package com.yunhe.task.task;

import com.yunhe.common.model.data.PointNumberDTO;
import com.yunhe.common.util.DateTimeUtils;
import com.yunhe.task.client.MeasHistoryClient;
import com.yunhe.task.client.MeasRealtimeClient;
import com.yunhe.task.config.DataMonitorConfig;
import com.yunhe.common.model.DomainList;
import com.yunhe.common.model.data.Measurement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.*;

@Service
public class DataMonitorTask {

    @Autowired
    private MeasHistoryClient measHistoryClient;
    @Autowired
    private MeasRealtimeClient realtimeClient;
    @Autowired
    private DataMonitorConfig dataMonitorConfig;


    private Long[] frequency15;
    private Long[] frequency1;

    @PostConstruct
    public void init() {
        frequency15 = dataMonitorConfig.getFrequency15();
        frequency1 = dataMonitorConfig.getFrequency1();
    }

    public void monitor15() {
        List<Measurement> list = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date date = calendar.getTime();
//        Set<Long> pnSet = new HashSet<>(Arrays.asList(frequency15));
//        List<Analog> analogs = (List<Analog>)analogService.findAnalogsByFrequency(15);
//        if (analogs != null && analogs.size() > 0) {
//            for (Analog analog : analogs) {
//                pnSet.setLanguage(analog.getPointNumber());
//            }
//        }
        List<Long> pnList = Arrays.asList(frequency15);
        PointNumberDTO pointNumberDTO = new PointNumberDTO();
        pointNumberDTO.setPointNumbers(pnList);
        Map<Long, Double> map = realtimeClient.getMeasureRealtime(pointNumberDTO).getBody().getResults();
        if (map != null) {
            for (Map.Entry<Long, Double> entry : map.entrySet()) {
                Measurement measurement = new Measurement();
                measurement.setDtime(DateTimeUtils.format(DateTimeUtils.PATTERN.DATETIME.value(), date));
                measurement.setPointNumber(entry.getKey());
                Double value = entry.getValue();
                if (value != null) {
                    measurement.setVal(value);
                    list.add(measurement);
                }
            }
        }
        if (list.size() > 0) {
            measHistoryClient.createMeasurementAsync(new DomainList<>(list));
        }
    }

    public void monitor1() {
        List<Measurement> list = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date date = calendar.getTime();
        Set<Long> pnSet = new HashSet<>(Arrays.asList(frequency1));
//        List<Analog> analogs = (List<Analog>)analogService.findAnalogsByFrequency(1);
//        if (analogs != null && analogs.size() > 0) {
//            for (Analog analog : analogs) {
//                pnSet.setLanguage(analog.getPointNumber());
//            }
//        }
        for (Long pn : pnSet) {
            try {
                PointNumberDTO pointNumberDTO = new PointNumberDTO();
                pointNumberDTO.setPointNumbers(Collections.singletonList(pn));
                Map<Long, Double> map = realtimeClient.getMeasureRealtime(pointNumberDTO).getBody().getResults();
                Measurement measurement = new Measurement();
                measurement.setDtime(DateTimeUtils.format(DateTimeUtils.PATTERN.DATETIME.value(), date));
                measurement.setPointNumber(pn);
                Double value = map.get(pn);
                measurement.setVal(value);
                list.add(measurement);
            } catch (Exception e) {
            }
        }
        measHistoryClient.createMeasurementAsync(new DomainList<>(list));
    }
}
